package com.ruoyi.system.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.ruoyi.system.domain.JacPrintModel;
import com.ruoyi.system.mapper.JacPrintModelMapper;

import com.ruoyi.system.service.IJacPrintModelService;
import com.ruoyi.common.core.text.Convert;

/**
 * 打印模板Service业务层处理
 * 
 * @author ruoyi
 * @date 2020-07-21
 */
@Service
public class JacPrintModelServiceImpl implements IJacPrintModelService 
{
    @Autowired
    private JacPrintModelMapper jacPrintModelMapper;

    /**
     * 查询打印模板
     * 
     * @param modelId 打印模板ID
     * @return 打印模板
     */
    @Override
    public JacPrintModel selectJacPrintModelById(Long modelId)
    {
        return jacPrintModelMapper.selectJacPrintModelById(modelId);
    }
    
    /**
     * 通过菜单和基地获取打印模板
     * 
     * @param factoryId menuId
     * @return 文件id 模板名称
     */
    @Override
    public List<JacPrintModel> selectModelByMeFa(JacPrintModel jacPrintModel)
    {
    	return jacPrintModelMapper.selectModelByMeFa(jacPrintModel);
    }
    /**
     * 查询打印模板列表
     * 
     * @param jacPrintModel 打印模板
     * @return 打印模板
     */
    @Override
    public List<JacPrintModel> selectJacPrintModelList(JacPrintModel jacPrintModel)
    {
        return jacPrintModelMapper.selectJacPrintModelList(jacPrintModel);
    }

    /**
     * 新增打印模板
     * 
     * @param jacPrintModel 打印模板
     * @return 结果
     */
    @Override
    public int insertJacPrintModel(JacPrintModel jacPrintModel)
    {   
    	//插入模板主数据
    	int rows = jacPrintModelMapper.insertJacPrintModel(jacPrintModel);
    	//插入模板与基地关系
    	insertJacPrintModelFactory(jacPrintModel);
        return rows;
    }
    
    
    /**
     * 新增打印模板
     * 
     * @param jacPrintModel 打印模板
     * @return 结果
     */
    
    public void insertJacPrintModelFactory(JacPrintModel jacPrintModel)
    {   
        List<Long> factoryIds = jacPrintModel.getFactoryIds();
        if (factoryIds.size() > 0) {
        	List<JacPrintModel> list = new ArrayList<JacPrintModel>();
        	for (Long factoryId : factoryIds) 
        	{   
        		JacPrintModel pm = new JacPrintModel();
        		pm.setmodelId(jacPrintModel.getmodelId());
        		pm.setFactoryId(factoryId);
        		list.add(pm);
        	}
        	if 	(list.size() > 0)
        	{
        		jacPrintModelMapper.batchModelFactory(list);
        	}
        }
    }

    /**
     * 修改打印模板
     * 
     * @param jacPrintModel 打印模板
     * @return 结果
     */
    @Override
    public int updateJacPrintModel(JacPrintModel jacPrintModel)
    {   
    	//删除模板与基地对应关系
    	jacPrintModelMapper.deleteModelFactoryById(jacPrintModel.getmodelId());
    	//插入模板与基地关系
    	insertJacPrintModelFactory(jacPrintModel);
        return jacPrintModelMapper.updateJacPrintModel(jacPrintModel);
    }
    
    /**
     * 修改模板状态
     * 
     * @param jacPrintModel 打印模板
     * @return 结果
     */
    @Override
    public int changeStatus(JacPrintModel jacPrintModel)
    {   
        return jacPrintModelMapper.updateJacPrintModel(jacPrintModel);
    }

    /**
     * 删除打印模板对象
     * 
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteJacPrintModelByIds(String ids)
    {   
        return jacPrintModelMapper.deleteJacPrintModelByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除打印模板信息
     * 
     * @param modelId 打印模板ID
     * @return 结果
     */
    public int deleteJacPrintModelById(Long modelId)
    {   
    	jacPrintModelMapper.deleteModelFactoryById(modelId);
        return jacPrintModelMapper.deleteJacPrintModelById(modelId);
    }
}
